//
//  YHHomeRateRevisionCell.m
//  YHApp
//
//  Created by -- on 2021/7/24.
//

#import "HARateRevisionCell.h"
#import "HAMachineRateMakeMo.h"
#import "YHMachineRateMakeRateCell.h"

@interface HARateRevisionCell ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation HARateRevisionCell

-(void)setModel:(HAMachineRateMakeMo *)model {
    _model = model;
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _model.rates.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    YHMachineRateMakeRatesModel *rateMo = _model.rates[indexPath.row];
    CGFloat h = 54;
    h += (54+28) * rateMo.item.count;
    return h;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YHMachineRateMakeRateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMachineRateMakeRateCellID"];
    if (!cell) {
        cell = [[YHMachineRateMakeRateCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHMachineRateMakeRateCellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.rateModel = _model.rates[indexPath.row];
    return cell;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_offset(54*3);
    }];
    
    UIView *top1View = [self getTopSubView:[UIColor khaBGColor] title:@"商户信息" content:@"" isLine:NO];
    [self.topView addSubview:top1View];
    [top1View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_offset(54);
    }];
    
    UIView *top2View = [self getTopSubView:[UIColor whiteColor] title:@"商户编号" content:self.model.customer[@"merchant_id"] isLine:YES];
    [self.topView addSubview:top2View];
    [top2View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(54);
        make.height.mas_offset(54);
    }];
    UIView *top3View = [self getTopSubView:[UIColor whiteColor] title:@"商户名称" content:self.model.customer[@"surname"] isLine:NO];
    [self.topView addSubview:top3View];
    [top3View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(54*2);
        make.height.mas_offset(54);
    }];

    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(54*3);
        make.bottom.mas_equalTo(0);
    }];
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.contentView addSubview:_tableView];
    }
    return _tableView;
}

- (UIView *)topView
{
    if (!_topView) {
        _topView = [UIView ha_createView];
        [self.contentView addSubview:_topView];
    }
    return _topView;
}

-(UIView *)getTopSubView:(UIColor *)bgColor title:(NSString *)title content:(NSString *)content isLine:(BOOL)isLine {
    UIView *view = [UIView ha_createView];
    view.backgroundColor = bgColor;
    
    if (isLine) {
        UIView *_lineView = [UIView ha_createView];
        _lineView.backgroundColor = [UIColor kLineColor];
        [view addSubview:_lineView];
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.height.mas_offset(0.8);
            make.bottom.mas_equalTo(0);
        }];
    }
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:16 textColor:@"#000000" text:title alignment:0];
    [view addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_offset(0);
    }];

    UILabel *conLabel = [UILabel ha_NLabelWithFont:16 textColor:@"#666666" text:content alignment:0];
    [view addSubview:conLabel];
    [conLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_offset(0);
    }];

    
    return  view;
}


@end
